package com.yunbiao.ybboardapi.backlight;

import android.app.smdt.SmdtManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.ys.rkapi.MyManager;
import com.yunbiao.ybboardapi.DeviceType;
import com.yunbiao.ybboardapi.utils.SystemPropertyUtils;

public class BackLightManager {
    private static final String TAG = "BackLightManager";
    private static BackLightManager backLightManager;

    private BackLightManager() {
    }

    public static BackLightManager getInstance() {
        if (backLightManager == null) {
            backLightManager = new BackLightManager();
        }
        return backLightManager;
    }

    public void enableBackLight(Context context,boolean enable, DeviceType deviceType) {
        DeviceType byType = DeviceType.getByType(deviceType.getType());
        Log.e(TAG, "enableBackLight: ------------byType----------->"+byType );
        switch (byType) {
            case SDMT:
                SmdtManager manager = SmdtManager.create(context);
                enable_smdt(manager,enable);
                break;
            case YISHENG:
                MyManager myManager = MyManager.getInstance(context);
                enable_ys(myManager,enable);
                break;
            case YISHENG_M:
                enable_ys(context,enable);
                break;
            case MSTAR:
                enable_qj(context,enable);
                break;
            default:
                Log.d(TAG, "getBackLightType: deviceType default");
                break;
        }
    }

    public int enableBackLightWithType(Context context,boolean enable, DeviceType deviceType) {
        enableBackLight(context,enable,deviceType);
        try {
            Thread.sleep(300);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return getBackLightType(context,deviceType);
    }

    public int getBackLightType(Context context, DeviceType deviceType) {
        int enable=-1;
        DeviceType byType = DeviceType.getByType(deviceType.getType());
        switch (byType) {
            case SDMT:
                SmdtManager manager = SmdtManager.create(context);
                if (manager!=null){
                    enable=manager.smdtGetLcdLightStatus();
                }
                break;
            case YISHENG:
                MyManager myManager = MyManager.getInstance(context);
                if (myManager!=null){
                    boolean backlightOn = myManager.isBacklightOn();
                    enable=backlightOn?1:0;
                }
                break;
            case MSTAR:
                enable= SystemPropertyUtils.getInstance().getInt("mstar.str.suspending",-1);
                break;
            default:
                Log.d(TAG, "getBackLightType: deviceType default");
                break;
        }
        return enable;
    }

    private void enable_qj(Context context, boolean enable) {
        if (enable) {
            Intent intent = new Intent("android.action.adtv.wakeup");
            context.sendBroadcast(intent);
        } else {
            Intent intent = new Intent("android.action.adtv.sleep");
            context.sendBroadcast(intent);
        }
    }

    private void enable_smdt(SmdtManager smdtManager, boolean enable) {
        if (smdtManager == null) return;
        if (enable) {
            smdtManager.smdtSetLcdBackLight(1);
        } else {
            smdtManager.smdtSetLcdBackLight(0);
        }
    }

    private void enable_ys(MyManager ysManager, boolean enable) {
        if (ysManager == null) return;
        if (enable) {
            ysManager.turnOnBackLight();
//            ysManager.turnOnHDMI();//开启hdmi输出信号
        } else {
            ysManager.turnOffBackLight();
//            ysManager.turnOffHDMI();//关闭hdmi输出信号
        }
    }
    private void enable_ys(Context context, boolean enable) {
        if (enable) {
            Intent intent = new Intent("com.ys.set_screen_on").setPackage("com.ys.ys_receiver");
            intent.putExtra("screen_on",1);
            context.sendBroadcast(intent);
        } else {
            Intent intent = new Intent("com.ys.set_screen_on").setPackage("com.ys.ys_receiver");
            intent.putExtra("screen_on",0);
            context.sendBroadcast(intent);
        }
    }
}
